#include "ScreenDrawing.h"
#include "ui_ScreenDrawing.h"
#include <QGraphicsOpacityEffect>
#include <QDebug>
#include <QScreen>

NewWhiteBoard::NewWhiteBoard(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::NewWhiteBoard)
{
    ui->setupUi(this);

    init();
}

NewWhiteBoard::~NewWhiteBoard()
{
    delete ui;

    if(paintListWidget)
        delete paintListWidget;
    paintListWidget = nullptr;

    if(ploygonWidget)
        delete ploygonWidget;
    ploygonWidget = nullptr;

    if(sizePropertyWidget)
        delete sizePropertyWidget;
    sizePropertyWidget = nullptr;

    if(fontColorWidget)
        delete fontColorWidget;
    fontColorWidget = nullptr;

    if(backgroundColorWidget)
        delete backgroundColorWidget;
    backgroundColorWidget = nullptr;
}

void NewWhiteBoard::init()
{
    // 界面相关初始化
    this->setWindowFlags(Qt::FramelessWindowHint);

//    setAttribute(Qt::WA_NoSystemBackground);
//    setAttribute(Qt::WA_TranslucentBackground);
//    setAttribute(Qt::WA_TransparentForMouseEvents);
//    setAutoFillBackground(false);
    setAttribute(Qt::WA_TranslucentBackground,true);
//    QPalette pal = palette();
//    pal.setColor(QPalette::Background, QColor(0,0,255,200));
//    setPalette(pal);

//    QScreen *screen = QGuiApplication::primaryScreen ();
//    QSize screenSize = screen->availableVirtualGeometry().size();

//    cacheMap = new QPixmap(screenSize);
//    cacheMap->fill(QColor(0, 0, 0, 1));

    // 对象相关初始化
    ploygonWidget = new PloygonWidget(this);
    sizePropertyWidget = new SizePropertyWidget(this);
    ploygonWidget->hide();

    sizePropertyWidget->hide();

    fontColorWidget = new ColorWidget(COLORS::COLOR_White,this);
    fontColorWidget->hide();

    backgroundColorWidget = new ColorWidget(COLORS::COLOR_Black,this);
    backgroundColorWidget->hide();

    paintListWidget = new PaintListWidget(this);
    paintListWidget->hide();

    // 信号初始化
    connect(fontColorWidget,&ColorWidget::ColorChange,this,&NewWhiteBoard::setPen);
    connect(sizePropertyWidget,&SizePropertyWidget::SizeChange,this,&NewWhiteBoard::setPen);
    connect(ploygonWidget,&PloygonWidget::PloygonTypeChange,this,&NewWhiteBoard::setPloygonType);
    connect(backgroundColorWidget,&ColorWidget::ColorChange,this,&NewWhiteBoard::ChangeBackground);
    connect(paintListWidget,&PaintListWidget::DeleteDown,this,&NewWhiteBoard::ListDeleteDown);
    connect(paintListWidget,&PaintListWidget::OpenShow,this,&NewWhiteBoard::OpenShowPaint);
    connect(ui->paintWidget,&PaintWidget::changeBackgroundColor,[=](const QColor &bck){ui->w->setPalette(bck);});
    connect(ui->paintWidget,&PaintWidget::LeftButtonPressed,[=](){AllPropertyWidgetHide();});
    connect(ui->paintWidget,&PaintWidget::setNormalEraser,[=](){setNormalEraser();});

    setWindowFlags(this->windowFlags() | Qt::WindowStaysOnTopHint);

}

//void NewWhiteBoard::changeBackgroundColor()

void NewWhiteBoard::on_Button_Pen_clicked()
{
    AllPropertyWidgetHide();

//    ui->w->setStyleSheet("background-color:black;");


    QColor p = QColor(0,0,0,1);
    ui->w->setPalette(p);
    isLucency = true;

    ui->paintWidget->setModelType(PW::ModelType::FREELINE);
    ChangeButtonFace(PW::BUTTONFACE::_PEN);
}

void NewWhiteBoard::on_Button_Ploygon_clicked()
{
    bool IsHide = ploygonWidget->isHidden();

    QColor p = QColor(0,0,0,0);
    ui->w->setPalette(p);

    AllPropertyWidgetHide();

    if(IsHide)
    {
        QSize PloygonSize= ploygonWidget->size();
        QPoint PloygonFrameGeometry = ui->PropertyWidget->pos() + ui->Button_Ploygon->pos();
        ploygonWidget->setGeometry(PloygonFrameGeometry.x(),ui->PropertyWidget->size().height()+10,PloygonSize.width(),PloygonSize.height());
        ploygonWidget->show();
    }
    else
        ploygonWidget->hide();

    ui->paintWidget->setModelType(PW::ModelType::PLOYGON);
    ChangeButtonFace(PW::BUTTONFACE::_PLOYGON);
}

void NewWhiteBoard::on_Button_Eraser_clicked()
{
    AllPropertyWidgetHide();

    if (isLucency) {
        ui->paintWidget->setModelType(PW::ModelType::LUCENCY);
        ChangeButtonFace(PW::BUTTONFACE::_ERASER);
    }else {
        ui->paintWidget->setModelType(PW::ModelType::ERASER);
        ChangeButtonFace(PW::BUTTONFACE::_ERASER);
    }

}

void NewWhiteBoard::on_Button_PenSIze_clicked()
{
    bool IsHide = sizePropertyWidget->isHidden();

    AllPropertyWidgetHide();

    if(IsHide)
    {
        QSize PloygonSize= sizePropertyWidget->size();
        QPoint PloygonFrameGeometry = ui->PropertyWidget->pos() + ui->Button_PenSIze->pos();
        sizePropertyWidget->setGeometry(PloygonFrameGeometry.x(),ui->PropertyWidget->size().height()+10,PloygonSize.width(),PloygonSize.height());
        sizePropertyWidget->show();
    }
    else
        sizePropertyWidget->hide();

    ChangeButtonFace(PW::BUTTONFACE::_PENSIZE);
}

void NewWhiteBoard::on_Button_PenColor_clicked()
{
    bool IsHide = fontColorWidget->isHidden();

    AllPropertyWidgetHide();

    if(IsHide)
    {
        QSize PloygonSize= fontColorWidget->size();
        QPoint PloygonFrameGeometry = ui->PropertyWidget->pos() + ui->Button_PenColor->pos();
        fontColorWidget->setGeometry(PloygonFrameGeometry.x(),ui->PropertyWidget->size().height()+10,PloygonSize.width(),PloygonSize.height());
        fontColorWidget->show();
    }
    else
        fontColorWidget->hide();

    ChangeButtonFace(PW::BUTTONFACE::_LINECOLOR);
}

void NewWhiteBoard::on_Button_PaintWidgetBackground_clicked()
{
    bool IsHide = backgroundColorWidget->isHidden();

    AllPropertyWidgetHide();

    if(IsHide)
    {
        QSize PloygonSize= backgroundColorWidget->size();
        QPoint PloygonFrameGeometry = ui->PropertyWidget->pos() + ui->Button_PaintWidgetBackground->pos();
        backgroundColorWidget->setGeometry(PloygonFrameGeometry.x(),ui->PropertyWidget->size().height()+10,PloygonSize.width(),PloygonSize.height());
        backgroundColorWidget->show();
    }
    else
        backgroundColorWidget->hide();

    ChangeButtonFace(PW::BUTTONFACE::_BACKGROUNDCOLOR);
}

void NewWhiteBoard::on_Button_Revocation_clicked()
{
    AllPropertyWidgetHide();

    ui->paintWidget->RevocationSingleStep();
    ChangeButtonFace(PW::BUTTONFACE::_UNDOCURVE);
}

void NewWhiteBoard::on_Button_PaintWidgetClear_clicked()
{
    AllPropertyWidgetHide();

    ui->paintWidget->ClearPaint();
    ChangeButtonFace(PW::BUTTONFACE::_CLEARCURVE);
}

void NewWhiteBoard::on_Button_PaintWidgetList_clicked()
{
    bool IsHide = paintListWidget->isHidden();

    AllPropertyWidgetHide();

    if(IsHide)
    {
        QSize PloygonSize= paintListWidget->size();
        QPoint PloygonFrameGeometry = ui->PropertyWidget->pos() + ui->Button_PaintWidgetList->pos();
        paintListWidget->setGeometry(PloygonFrameGeometry.x(),ui->PropertyWidget->size().height()+10,PloygonSize.width(),PloygonSize.height());

        // 对列表窗口操作
        paintListWidget->ListWidgetInit(ui->paintWidget->GetFilePtr()->FindQuote());

        paintListWidget->show();
    }
    else
        paintListWidget->hide();

    ChangeButtonFace(PW::BUTTONFACE::_PAINTLISE);
}

void NewWhiteBoard::on_Button_PaintWidgetSave_clicked()
{
    AllPropertyWidgetHide();

    ui->paintWidget->SaveData();
    ChangeButtonFace(PW::BUTTONFACE::_PAINTSAVE);
}

void NewWhiteBoard::on_Button_Exit_clicked()
{
    AllPropertyWidgetHide();
    ChangeButtonFace(PW::BUTTONFACE::_PAINTCLOSE);

    close();
}

void NewWhiteBoard::OpenShowPaint(const QString &tag)
{
    ui->paintWidget->ReadFileData(tag);
}

void NewWhiteBoard::ListDeleteDown(const QString &tag)
{
    ui->paintWidget->GetFilePtr()->DeleteSingleWidgetInfo(tag);

    paintListWidget->hide();
    on_Button_PaintWidgetList_clicked();
}

void NewWhiteBoard::ChangeBackground()
{
    QColor Color = backgroundColorWidget->GetColor();
    ui->paintWidget->setBackgroundColor(Color);
}

void NewWhiteBoard::AllPropertyWidgetHide()
{
    if(ploygonWidget && !ploygonWidget->isHidden())
        ploygonWidget->hide();

    if(sizePropertyWidget && !sizePropertyWidget->isHidden())
        sizePropertyWidget->hide();

    if(fontColorWidget && !fontColorWidget->isHidden())
        fontColorWidget->hide();

    if(backgroundColorWidget && !backgroundColorWidget->isHidden())
        backgroundColorWidget->hide();

    if(paintListWidget && !paintListWidget->isHidden())
        paintListWidget->hide();
}

void NewWhiteBoard::setNormalEraser() {
    isLucency = false;
}

void NewWhiteBoard::ChangeButtonFace(const PW::BUTTONFACE &bf)
{
    ui->Button_Pen->setStyleSheet("border-image: url(:/PropertyWidget/Resource/pen_1.png);");
    ui->Button_Ploygon->setStyleSheet("border-image: url(:/PropertyWidget/Resource/polygon_1.png);");
    ui->Button_Eraser->setStyleSheet("border-image: url(:/PropertyWidget/Resource/eraser_1.png);");
    ui->Button_PenSIze->setStyleSheet("border-image: url(:/PropertyWidget/Resource/size_1.png);");
    ui->Button_PenColor->setStyleSheet("border-image: url(:/PropertyWidget/Resource/color_1.png);");
    ui->Button_PaintWidgetBackground->setStyleSheet("border-image: url(:/PropertyWidget/Resource/Background_1.png);");
    ui->Button_Revocation->setStyleSheet("border-image: url(:/PropertyWidget/Resource/undo_1.png);");
    ui->Button_PaintWidgetClear->setStyleSheet("border-image: url(:/PropertyWidget/Resource/clearCurve_1.png);");
    ui->Button_PaintWidgetList->setStyleSheet("border-image: url(:/PropertyWidget/Resource/list_1.png);");
    ui->Button_PaintWidgetSave->setStyleSheet("border-image: url(:/PropertyWidget/Resource/save_1.png);");
    ui->Button_Exit->setStyleSheet("border-image: url(:/PropertyWidget/Resource/close_1.png);");

    if(bf == PW::BUTTONFACE::_PEN)
        ui->Button_Pen->setStyleSheet("border-image: url(:/PropertyWidget/Resource/pen_2.png);");
    else if(bf == PW::BUTTONFACE::_PLOYGON)
        ui->Button_Ploygon->setStyleSheet("border-image: url(:/PropertyWidget/Resource/ploygon_2.png);");
    else if(bf == PW::BUTTONFACE::_ERASER)
        ui->Button_Eraser->setStyleSheet("border-image: url(:/PropertyWidget/Resource/eraser_2.png);");
    else if(bf == PW::BUTTONFACE::_PENSIZE)
        ui->Button_PenSIze->setStyleSheet("border-image: url(:/PropertyWidget/Resource/size_2.png);");
    else if(bf == PW::BUTTONFACE::_LINECOLOR)
        ui->Button_PenColor->setStyleSheet("border-image: url(:/PropertyWidget/Resource/color_2.png);");
    else if(bf == PW::BUTTONFACE::_BACKGROUNDCOLOR)
        ui->Button_PaintWidgetBackground->setStyleSheet("border-image: url(:/PropertyWidget/Resource/Background_2.png);");
    else if(bf == PW::BUTTONFACE::_UNDOCURVE)
        ui->Button_Revocation->setStyleSheet("border-image: url(:/PropertyWidget/Resource/undo_2.png);");
    else if(bf == PW::BUTTONFACE::_CLEARCURVE)
        ui->Button_PaintWidgetClear->setStyleSheet("border-image: url(:/PropertyWidget/Resource/clearCurve_2.png);");
    else if(bf == PW::BUTTONFACE::_PAINTLISE)
        ui->Button_PaintWidgetList->setStyleSheet("border-image: url(:/PropertyWidget/Resource/list_2.png);");
    else if(bf == PW::BUTTONFACE::_PAINTSAVE)
        ui->Button_PaintWidgetSave->setStyleSheet("border-image: url(:/PropertyWidget/Resource/save_2.png);");
    else if(bf == PW::BUTTONFACE::_PAINTCLOSE)
        ui->Button_Exit->setStyleSheet("border-image: url(:/PropertyWidget/Resource/close_2.png);");
}

void NewWhiteBoard::setPen()
{
    QPen pen(fontColorWidget->GetColor(),sizePropertyWidget->GetSize());
    ui->paintWidget->setPen(pen);
}

void NewWhiteBoard::setPloygonType()
{
    ui->paintWidget->setPloygonType(ploygonWidget->GetPloygonType());
}

void NewWhiteBoard::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
//    painter.drawPixmap(0, 0, *cacheMap);
//    painter.fillRect(this->rect(),QColor(0,0,0,1));
    QPen p;
    p.setWidth(5);
    p.setColor(QColor(67, 129, 255));
    painter.setPen(p);
    painter.drawLine(0, 0, this->width() - 1, 0);
    painter.drawLine(0, 0, 0, this->height() - 1);
    painter.drawLine(this->width() - 1, 0, this->width() - 1, this->height() - 1);
    painter.drawLine(0, this->height() - 1, this->width() - 1, this->height() - 1);
}
